package pCloud.config;

import java.io.File;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * Provide access to the experiment configuration: + policyFile = <list of
 * policy files> + requestFile = request file
 * 
 * @author anhdinh
 * 
 */
public class PCloudConfig {

	static Configuration config;

	public static final String DEFAULT_CONFIG_FILE = "defaultConfig";

	static final String POLICY_FILE = "policyFile";
	static final String POLICY_FILE_DIR = "policyFile_Dir";
	static final String REQUEST_FILE = "requestFile";
	static final String REQUEST_FILE_DIR = "requestFile_Dir";
	static final String USER_NAME = "username";
	static final String PASSWORD = "password";
	static final String DRIVER = "dbdriver";
	
	//wwq: stream server configurations
	
	static final String STREAM_SCHEMA_DIR = "streamSchema_Dir";
	static final String STREAM_ENGINE_ADDRESS = "stream_engine_address";
	static final String STREAM_ENGINE_PORT = "stream_engine_port";

	public static final String SHOW_TABLE_QUERY ="\\d";
	public static final String SHOW_COLUMN_QUERY = "\\d"; 
	public static final String PCLOUD_DATA_MAPPING_FILE = "data_map_file";
	public static final String PCLOUD_NDATASETS = "nDataSets";
	
	public static final String PCLOUD_DATA_SERVER_ID = "pcloud_data_server_id";
	
	public static final String PCLOUD_PROXY_SERVER_LOG = "pcloud_proxy_server_log";
	public static final String PCLOUD_CLIENT_LOG = "pcloud_client_log";
	public static final String PCLOUD_DATA_SERVER_LOG = "pcloud_data_server_log";
	
	public static final String PCLOUD_PROXY_SERVER_ADDRESS = "pcloud_proxy_server_address";
	public static final String PCLOUD_DATA_SERVER_ADDRESS = "pcloud_data_server_address";

	public static final String PCLOUD_DATA_SERVER_DATABASE = "pcloud_data_server_database";
	
	public static final String PCLOUD_PROXY_SERVER_PROT = "pcloud_proxy_server_prot";
	public static final String PCLOUD_DATA_SERVER_PROT = "pcloud_data_server_prot";
	public static final String PCLOUD_DATABASE_PROT = "pcloud_database_prot";
	public static final String PCLOUD_CLIENT_PROT = "pcloud_client_prot";
	public static final String PCLOUD_PROXY_SERVER_CLIENT_PROT = "pcloud_proxy_server_client_prot";

	public static final String PCLOUD_ACCESS_CONTROL_PROT = "pcloud_access_control_prot";
	
	public static final String PCLOUD_TRANSPORT_SERVER = "pcloud_transport_server";
	public static final String PCLOUD_TRANSPORT_CLIENT = "pcloud_transport_client";

	public static final String PCLOUD_CACHE_OPTION = "pcloud_cache_option";
	
	//wwq: stream server configurations
	
	public static final String PCLOUD_STREAM_ENGINE_PROT = "pcloud_datastream_prot";

	
	
	public static boolean containsField(String key){
		//System.out.println("File containing key "+key+" = "+config.containsKey(key));
		return config.containsKey(key);
	}
	
	public static void init(String configFile) throws Exception {
		config = new PropertiesConfiguration(configFile);
	}

	public static boolean getBoolean(String field){
		return config.getBoolean(field);
	}
	
	public static String getString(String field) {		
		return config.getString(field);
	}

	/**
	 * @return a list of policy files, if a directory is given then returns the
	 *         list of files contained in the directory.
	 */
	public static String[] getPolicyFiles() {
		String[] sa = config.getStringArray(POLICY_FILE);
		if (sa.length == 0) {
			String directory = config.getString(POLICY_FILE_DIR);
			File dir = new File(directory);
			sa = dir.list();
			for (int i = 0; i < sa.length; i++)
				sa[i] = directory + "/" + sa[i];
		}
		return sa;
	}

	/**
	 * @return a list of request files, similar to {@link #getPolicyFiles()}
	 */
	public static String[] getRequestFiles() {
		String[] sa = config.getStringArray(REQUEST_FILE);
		if (sa.length == 0) {
			String directory = config.getString(REQUEST_FILE_DIR);
			File dir = new File(directory);
			sa = dir.list();
			for (int i = 0; i < sa.length; i++)
				sa[i] = directory + "/" + sa[i];
		}
		return sa;
	}

	//wwq: stream engine configs 
	//start
	public static String getStreamEngineAddress() {
		return config.getString(STREAM_ENGINE_ADDRESS);
	}
	
	public static String getStreamEnginePort() {
		return config.getString(STREAM_ENGINE_PORT);
	}
	
	public static String getStreamSchemaDir(){
		return config.getString(STREAM_SCHEMA_DIR);
	}
	
	// end
	
	public static String getDatabaseUserName() {
		return config.getString(USER_NAME);
	}

	public static String getDatabasePassword() {
		return config.getString(PASSWORD);
	}

	public static String getDatabaseDriver() {
		return config.getString(DRIVER);
	}

	public static String getPCloudServerAddress() {
		return config.getString(PCLOUD_PROXY_SERVER_ADDRESS);
	}
}
